/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.syndance.aplicativo.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import javax.swing.JOptionPane;
import org.syndance.aplicativo.interfaces.IDaoCurso;
import org.sysdance.aplicativo.modelo.Curso;

/**
 *
 * @author gocard
 */
public class DaoCurso implements IDaoCurso {

    private static DaoCurso daoCurso = null;

    public DaoCurso() {
    }

    public static DaoCurso getInstance() {
        if (daoCurso == null) {
            daoCurso = new DaoCurso();
        }
        return daoCurso;
    }

    @Override
    public Curso getByCurso(Curso object) throws Exception {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public int getQuantCurso() throws Exception {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void salva(Curso object) throws Exception {

        Connection connection = Dao.getInstance().getInstanceConnection();
        try {
            connection.setAutoCommit(false);
            String commandSQL;
            if (object.getId() == null) {

                commandSQL = "INSERT INTO curso( nome, idade, tempo, valor) " + " VALUES ( ?, ?, ?, ?)";
            } else {

                commandSQL = "  UPDATE curso SET  nome=?, idade=?, tempo=?, valor=?  WHERE id = " + object.getId();
            }

            PreparedStatement preparedStatement;
            if (object.getId() == null) {
                preparedStatement = connection.prepareStatement(commandSQL, Statement.RETURN_GENERATED_KEYS);
            } else {
                preparedStatement = connection.prepareStatement(commandSQL);
            }

            preparedStatement.setString(1, object.getNome());
            preparedStatement.setInt(2, object.getIdade());
            preparedStatement.setInt(3, object.getTempo());
            preparedStatement.setDouble(4, object.getValor());

            //preparedStatement.setDouble(4, NumberFormat.getCurrencyInstance().format(object.getValor()));
            preparedStatement.executeUpdate();

            if (object.getId() == null) {
                try (ResultSet resultSet = preparedStatement.getGeneratedKeys()) {
                    if (resultSet.next()) {
                        object.setId(resultSet.getLong("id"));
                    }
                }
            }

            preparedStatement.close();

            connection.commit();
        } catch (SQLException e) {
            connection.rollback();
            StringBuilder mensagem = new StringBuilder();
            mensagem.append("Não foi possível salvar o usuário no banco de dados2.").append("\nMotivo: ").append(e.getMessage());
            throw new Exception(mensagem.toString());
        } finally {
            Dao.getInstance().closeConnection();
        }

    }

    @Override
    public void deleta(Curso object) throws Exception {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Curso getById(Curso object) throws Exception {

        Connection connection = Dao.getInstance().getInstanceConnection();
        String commandSQL = "select * from curso where id = ?";
        PreparedStatement preparedStatement = connection.prepareStatement(commandSQL);
        ResultSet resultset = null;
        try {
            preparedStatement.setLong(1, object.getId());
            resultset = preparedStatement.executeQuery();
            if (resultset.next()) {
                object.setId(resultset.getLong("id"));
                object.setNome(resultset.getString("nome"));
                object.setIdade(resultset.getInt("idade"));
                object.setTempo(resultset.getInt("tempo"));
                object.setValor(resultset.getDouble("valor"));
                ;

                return object;
            } else {
                return null;
            }
        } catch (SQLException e) {
            StringBuilder mensagem = new StringBuilder();
            mensagem.append("Não foi possível selecionar o usuário no banco de dados.").append("\nMotivo: ").append(e.getMessage());
            throw new Exception(mensagem.toString());
        } finally {
            resultset.close();
            preparedStatement.close();
            Dao.getInstance().closeConnection();
        }

    }

    /*    @Override
     public Object listagem() {
     throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
     }*/
    
    @Override
    public Object checkIntegrityReferential(Curso object) throws Exception {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

}
